package com.example.springboottest.example.threadlocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalTest {
    private static Integer total = 0;
    private static ThreadLocal<Integer> totalThreadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(20);

        for (int i = 0; i < 20; i++) {
            int num = i;
            service.execute(() -> {
                total += 10;
                if (totalThreadLocal.get() == null)
                    totalThreadLocal.set(0);
                totalThreadLocal.set(totalThreadLocal.get() + 10);
//                System.out.println("第[" + num + "]个线程的\t" + "total=\t" + total);
                System.out.println("第[" + num + "]个线程的\t" + "totalThreadLocal=\t" + totalThreadLocal.get());
            });
        }
        service.shutdown();
    }
}
